package com.example.echoewish.ui.my_activity.user_info_activity;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.echoewish.R;
import com.example.echoewish.pojo.Friend;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.pojo.User;
import com.example.echoewish.util.DataGenerator;
import com.example.echoewish.ui.EchoWishCompatActivity;

public class UserInfoShowActivity extends EchoWishCompatActivity {
    private static int GET_FRIEND_SUCCESS=3;
    private static int GET_FRIEND_FAIL=4;
    private static int CONCERN_FRIEND_SUCCESS=5;
    private static int CONCERN_FRIEND_FAIL=6;
    private static int CANCEL_CONCERN_FRIEND_SUCCESS=7;
    private static int CANCEL_CONCERN_FRIEND_FAIL=8;

    private ImageView headImageView;
    private TextView nameTextView;
    private TextView sexTextView;
    private TextView campusTextView;
    private TextView gradeTextView;
    private Button editUserInfoButton;
    private Button concernFriendsButton;
    private Button cancelConcernFriendsButton;

    private UserInfoShowViewModel userInfoShowViewModel;

    private Handler userInfoShowHandler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE:
                    if(msg.arg1==GET_FRIEND_SUCCESS)
                    {
                     initViewByFriend();
                    }
                    else if(msg.arg1==CONCERN_FRIEND_SUCCESS)
                    {
                        Toast.makeText(UserInfoShowActivity.this,"发送添加好友请求成功",Toast.LENGTH_SHORT).show();
                    }
                    else if(msg.arg1==CANCEL_CONCERN_FRIEND_SUCCESS)
                    {
                        concernFriendsButton.setVisibility(View.VISIBLE);
                        cancelConcernFriendsButton.setVisibility(View.GONE);
                        Toast.makeText(UserInfoShowActivity.this,"删除好友成功",Toast.LENGTH_SHORT).show();
                    }
                    else if(msg.arg1==GET_FRIEND_FAIL||msg.arg1==CONCERN_FRIEND_FAIL||msg.arg1==CANCEL_CONCERN_FRIEND_FAIL)
                    {
                        Toast.makeText(UserInfoShowActivity.this,"app出现异常",Toast.LENGTH_SHORT).show();
                    }
                    break;
                case Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE:
                    Toast.makeText(UserInfoShowActivity.this,"网络连接错误",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userInfoShowViewModel.userIdMutableLiveData.setValue(getIntent().getIntExtra("userId",-1));
        System.out.println(userInfoShowViewModel.userIdMutableLiveData.getValue());
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_user_info_show);
    }

    public void initView()
    {
        //初始化控件
        headImageView=findViewById(R.id.info_show_head_image_view);
        nameTextView=findViewById(R.id.info_show_name_text_view);
        sexTextView=findViewById(R.id.info_show_sex_text_view);
        campusTextView=findViewById(R.id.info_show_campus_text_view);
        gradeTextView=findViewById(R.id.info_show_grade_text_view);
        editUserInfoButton=findViewById(R.id.info_show_edit_button);
        concernFriendsButton=findViewById(R.id.concern_button);
        cancelConcernFriendsButton=findViewById(R.id.cancel_concern_button);

    }

    @Override
    public void initViewModel() {
        userInfoShowViewModel= new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(UserInfoShowViewModel.class);
    }

    @Override
    public void observeLiveData() {
        userInfoShowViewModel.userMutableLiveData.observe(this, new Observer<Friend>() {
            @Override
            public void onChanged(Friend user) {
                Message msg=new Message();
                if(user!=null)
                {
                    msg.what=Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                    msg.arg1=GET_FRIEND_SUCCESS;
                }
                else
                {
                    msg.what=Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                    msg.arg1=GET_FRIEND_FAIL;
                }
                userInfoShowHandler.sendMessage(msg);
            }
        });

        userInfoShowViewModel.responseMutableLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Message msg=new Message();
                msg.what=Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                if(concernFriendsButton.getVisibility()==View.GONE)
                {
                    if(s.equals(Constant.HTTPS_GET_CALLBACK_SUCCESS))
                    {
                        msg.arg1=CANCEL_CONCERN_FRIEND_SUCCESS;
                    }
                    else
                    {
                        msg.arg1=CANCEL_CONCERN_FRIEND_FAIL;
                    }
                }
                else
                {
                    if(s.equals(Constant.HTTPS_GET_CALLBACK_SUCCESS))
                    {
                        msg.arg1=CONCERN_FRIEND_SUCCESS;
                    }
                    else
                    {
                        msg.arg1=CONCERN_FRIEND_FAIL;
                    }
                }
                userInfoShowHandler.sendMessage(msg);
            }
        });

        userInfoShowViewModel.userIdMutableLiveData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if(integer!=UserInfo.getInstance().UserId&&integer!=-1)
                    userInfoShowViewModel.getUserInfo();
                else
                    initMyMessage();
            }
        });
    }

    private void initViewByFriend()
    {
        Friend friend=userInfoShowViewModel.userMutableLiveData.getValue();
        nameTextView.setText(friend.name);
        sexTextView.setText(DataGenerator.sexSelected[friend.sex+1]);
        campusTextView.setText(friend.campus);
        gradeTextView.setText(friend.grade+"级");
        editUserInfoButton.setVisibility(View.GONE);

        if(friend.headImage!=null&&!friend.headImage.equals(""))
            Glide.with(this).load(Constant.HEAD_IMAGE_URL+UserInfo.getInstance().headImage).into(headImageView);
        else
            Glide.with(this).load(R.drawable.no_load).into(headImageView);

        cancelConcernFriendsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userInfoShowViewModel.cancelConcernFriend(UserInfo.getInstance().UserId,userInfoShowViewModel.userIdMutableLiveData.getValue());
            }
        });

        concernFriendsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userInfoShowViewModel.concernFriend(UserInfo.getInstance().UserId,userInfoShowViewModel.userIdMutableLiveData.getValue());
            }
        });

        if(!friend.isConcern)
        {
            concernFriendsButton.setVisibility(View.VISIBLE);
            cancelConcernFriendsButton.setVisibility(View.GONE);
        }
        else
        {   cancelConcernFriendsButton.setVisibility(View.VISIBLE);
            concernFriendsButton.setVisibility(View.GONE);
        }
    }

    private void initMyMessage()
    {
        //如果是本人信息
            if(UserInfo.getInstance().headImage!=null&&!UserInfo.getInstance().headImage.equals(""))
                Glide.with(this).load(Constant.HEAD_IMAGE_URL+UserInfo.getInstance().headImage).into(headImageView);
            else
                Glide.with(this).load(R.drawable.no_load).into(headImageView);
            nameTextView.setText(UserInfo.getInstance().name);
            sexTextView.setText(DataGenerator.sexSelected[UserInfo.getInstance().sex+1]);
            campusTextView.setText(UserInfo.getInstance().campus);
            gradeTextView.setText(UserInfo.getInstance().grade+"级");
            editUserInfoButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(UserInfoShowActivity.this, UserInfoEditActivity.class);
                    startActivity(intent);
                }
            });
    }

    public static void activityStart(Context context,int userId)
    {
        Intent intent=new Intent(context,UserInfoShowActivity.class);
        intent.putExtra("userId",userId);
        context.startActivity(intent);
    }
}